<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>高校宿舍管理系统-卫生检查列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="shortcut icon" href="#"/>
    <link rel="stylesheet" href="../layui/lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../layui/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm data-add-btn" lay-event="add"><i
                        class="layui-icon layui-icon-addition"></i>添加
                </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit"><i
                    class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete"><i
                    class="layui-icon layui-icon-delete"></i>删除</a>
        </script>

        <!-- 修改窗口 -->
        <div style="display: none;padding: 5px" id="UpdateWindow">
            <form class="layui-form" style="width:90%;" id="dataFrm" lay-filter="dataFrm">
                <div class="layui-form-item">
                    <label class="layui-form-label">宿舍编号</label>
                    <div class="layui-input-block">
                        <input type="hidden" name="id">
                        <input type="text" name="brcode" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">寝室</label>
                    <div class="layui-input-block">
                        <input type="text" name="brrid" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">检查时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="time" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">检查内容</label>
                    <div class="layui-input-block">
                        <input type="text" name="content" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">打分</label>
                    <div class="layui-input-block">
                        <input type="text" name="grade" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">检查人</label>
                    <div class="layui-input-block">
                        <input type="text" name="checker" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item layui-row layui-col-xs12">
                    <div class="layui-input-block" style="text-align: center;">
                    <button type="button" class="layui-btn" lay-submit lay-filter="doUpdateSubmit"
                            style="margin-right: 20px">
                        <span class="layui-icon layui-icon-add-1"></span>提交
                    </button>
                    <button type="reset" class="layui-btn layui-btn-warm">
                        <span class="layui-icon layui-icon-refresh-1"></span>重置
                    </button>
                </div>
                </div>
            </form>
        </div>

    </div>
</div>
<script src="../layui/lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    var tableIns;

    layui.use(['form', 'table', 'layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            layer = layui.layer;

        tableIns = table.render({
            elem: '#currentTableId',
            url: '/dm/clean/list',
            toolbar: '#toolbarDemo',
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 100, title: 'ID', align: "center"},
                    {field: 'brcode', width: 130, title: '宿舍编号', align: "center"},
                    {field: 'brarea', width: 100, title: '宿舍区', align: "center"},
                    {field: 'brrid', width: 100, title: '寝室', align: "center"},
                    {field: 'content', title: '检查内容', minWidth: 180},
                    {field: 'grade', width: 80, title: '打分', align: "center"},
                    {field: 'time', width: 140, title: '检查时间', align: "center"},
                    {field: 'checker', width: 120, title: '检查人', align: "center"},
                    {title: '操作', width: 150, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [5, 10, 20, 25, 50, 100],
            limit: 5,
            page: true,
            skin: 'line'
        });

        var windowIndex;//窗口的索引

        // 打开添加页面
        function openAddWindow() {
            var index = layer.open({
                title: '添加卫生检查记录',
                type: 2,
                shade: 0.2,
                maxmin: false,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/dm/clean/add.html',
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
            return false;
        }

        //表格头部工具栏触发事件
        table.on('toolbar(currentTableFilter)', function (obj) {
            switch (obj.event) {
                case 'add':
                    openAddWindow();// 打开添加窗口
                    break;
            };
        });

        // 打开修改窗口
        function openUpdateWindow(data) {
            windowIndex = layer.open({
                type: 1,
                title: "修改卫生检查信息",
                area: ["700px", "450px"],
                content: $("#UpdateWindow"),
                success: function () {
                    // 表单数据回显
                    form.val("dataFrm", data);
                }
            });
        }

        // 监听修改窗口中表单提交事件
        form.on("submit(doUpdateSubmit)", function (data) {
            console.log(data.field);
            var url = "/dm/clean/update";
            $.post(url, data.field, function (result) {
                if (result.success) {
                    //刷新数据表格
                    tableIns.reload();
                    //关闭窗口
                    layer.close(windowIndex);
                }
                //提示信息
                layer.msg(result.msg);
            }, "json");
            //禁止页面刷新
            return false;
        });

        function deleteById(data) {
            console.log(data.id);
            layer.confirm('确定删除吗？', {icon: 3, title:'提示'}, function(index){
                //发送ajax请求进行删除
                $.post("/dm/clean/delete",{"id":data.id},function(result){
                    if(result.success){
                        tableIns.reload();
                    }
                    layer.msg(result.msg);
                },"json");
                layer.close(index);
            });
        }

        // 监听行工具栏事件
        table.on('tool(currentTableFilter)', function (obj) {
            switch (obj.event) {
                // 修改
                case 'edit':
                    openUpdateWindow(obj.data);
                    break;
                // 删除
                case 'delete':
                    deleteById(obj.data);
                    break;
            }
        });
    });
</script>

</body>
</html>